﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.WindowsAzure.ServiceRuntime;

using Domain;

namespace Repositories
{
	public class UserRepository
	{
		string connStr;


		public SqlConnection Connection { get; set; }


		public SqlConnection CreateConnection()
		{
			connStr = RoleEnvironment.GetConfigurationSettingValue("ConnectionString");

			Connection = new SqlConnection(connStr);

			return Connection;
		}

		public void BeginTransaction()
		{
			Connection.BeginTransaction();
		}


		public UserRepository()
		{
			CreateConnection();
		}





		public DataSet getUser(string userID, string username)
		{
			SqlConnection con = new SqlConnection(connStr);

			SqlDataAdapter da = new SqlDataAdapter();
			da.SelectCommand = new SqlCommand("GetPosts", con);
			da.SelectCommand.CommandType = CommandType.StoredProcedure;
			da.SelectCommand.Parameters.Add(new SqlParameter("userID", userID));
			da.SelectCommand.Parameters.Add(new SqlParameter("username", username));

			DataSet dst = new DataSet();

			con.Open();
			da.Fill(dst);
			con.Close();

			return dst;
		}



		public User getUser(string username)
		{
			User u = new User();

			SqlConnection conn = new SqlConnection(connStr);
			SqlCommand cmd = new SqlCommand();
			cmd.Connection = conn;
			cmd.CommandText = "GetUserDetails";
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("username", username));

			conn.Open();

			try
			{
				SqlDataReader dr = cmd.ExecuteReader();

				while (dr.Read())
				{
					u.ID = dr["id"].ToString();
					u.Name = dr["username"].ToString();
					u.RegisteredOn = DateTime.Parse(dr["RegisteredOn"].ToString());
					u.Avatar = dr["avatar"].ToString();
					u.Signature = dr["Signature"].ToString();
					u.Subtitle = dr["Subtitle"].ToString();
					u.Twitter = dr["Twitter"].ToString();
					u.LevelName = dr["LevelName"].ToString();
					u.LevelNr = dr["LevelNr"].ToString();
					u.Role = dr["Role"].ToString();
					u.PostsPerDay = dr["PostsPerDay"].ToString();

					if (dr["PostCount"].ToString() != "")
					{
						u.PostCount = Int64.Parse(dr["PostCount"].ToString());
					}

					if (dr["IsPWChanged"].ToString() == "true")
					{
						u.IsPWChanged = true;
					}
					else
					{
						u.IsPWChanged = false;
					}
				}
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				conn.Close();
				conn.Dispose();

				throw ex;
			}

			return u;
		}





		public void InsertUser(string username, string password)
		{
			SqlCommand cmd = null;

			cmd = new SqlCommand("InsertUser", Connection);

			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("username", username));
			cmd.Parameters.Add(new SqlParameter("password", password));

			Connection.Open();

			cmd.ExecuteNonQuery();
			Connection.Close();
		}


		/// <summary>
		/// Updates a specific user record in the database
		/// </summary>
		/// <param name="username"></param>
		/// <param name="password"></param>
		public void UpdateUser(string username, string password)
		{
			SqlCommand cmd = null;

			cmd = new SqlCommand("UpdateUser", Connection);

			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("username", username));
			cmd.Parameters.Add(new SqlParameter("password", password));

			Connection.Open();

			cmd.ExecuteNonQuery();
			Connection.Close();
		}



	}
}